package com.sbeam.controller;

import com.sbeam.pojo.Factory;
import com.sbeam.service.FactoryService;
import com.sbeam.service.GamesService;
import com.sbeam.vo.PageResult;
import com.sbeam.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/factory")
@CrossOrigin
public class FactoryController {

    @Autowired
    private FactoryService factoryService;

    @GetMapping("/getFactoryList")
    public SysResult getFactoryList(){
        List<Factory> list = factoryService.getFactoryList();
        if (list.isEmpty()){
            return SysResult.failed();
        }
        return SysResult.success(list);
    }

    @GetMapping("/findFactoryByName")
    public SysResult findFactoryByName(String searchKey){
        List<Factory> factoryList = factoryService.findFactoryByName(searchKey);
        if (factoryList.isEmpty()){
            return SysResult.failed();
        }
        return SysResult.success(factoryList);
    }

    @PostMapping("/addFactory")
    public SysResult addFactory(@RequestBody Factory factory){
        int result = factoryService.addFactory(factory);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @PutMapping("/updateFactoryStatus")
    public SysResult updateFactoryStatus(@RequestBody Factory factory){
        int result = factoryService.updateFactoryStatus(factory);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @PutMapping("/updateFactory")
    public SysResult updateFactory(@RequestBody Factory factory){
        int result = factoryService.updateFactory(factory);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }

    @DeleteMapping("/deleteFactoryById")
    public SysResult deleteFactoryById(Integer id){
        int result = factoryService.deleteFactoryById(id);
        if (result == 1){
            return SysResult.success();
        }
        return SysResult.failed();
    }
    /*厂商页面
.请求路径：/factory/getFactory
.请求方式：get
.请求参数：使用pageResult接收
.响应数据：SysResult对象
*/
    @Autowired
    private GamesService gamesService;

    @GetMapping("getFactory")
    public SysResult getFactory(PageResult pageResult){
        pageResult = factoryService.getFactory(pageResult);
        return SysResult.success(pageResult);
    }

}
